<template>
  <div>
    <el-card class="login-card" :body-style="{ display: 'flex', justifyContent: 'center', alignItems: 'center', padding: '60px'}">
      <div slot="header">
          <span><b>你画我猜</b></span>
          <el-tooltip class="item" effect="dark" content="人生苦短，何妨一玩" placement="top-start">
            <el-button class="msg-btn" type="text" icon="el-icon-message-solid"></el-button>
          </el-tooltip>
      </div>
      <el-form ref="loginForm" inline :model="formData" :rules="rules">
        <el-form-item prop="nickname">
          <el-input v-model="formData.nickname" placeholder="请输入您的昵称" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="enterGame">进入游戏</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { MessageBox } from 'element-ui'
import { mapActions } from 'vuex'
export default {
  data() {
    return {
      // 表单信息
      formData: {
        nickname: ''
      },

      // 校验规则
      rules: {
        nickname: [{ required: true, message: '请输入您的昵称' }]
      }
    }
  },

  methods: {

    ...mapActions(['checkUserExist']),

    enterGame() {
      this.$refs.loginForm.validate(async flag => {
        if (!flag) return 
        let nickname = this.formData.nickname
        let isUserExist = await this.checkUserExist(nickname)
        if (isUserExist) {
          MessageBox.alert("该昵称已被人使用啦!");
        } else {
          localStorage.setItem('nickname', nickname)
          this.$router.push('/home')
        }
      })
    }
  }
}
</script>

<style scoped>
.login-card {
  width: 50%;
  margin: 0 auto;
  margin-top: 200px;
}
.msg-btn {
  float: right; 
  padding: 3px 0
} 
</style>